﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.SYS.ViewModel;
using KPIS.GERP.WEB.GAMS.Class;

namespace KPIS.GERP.HtmlHelpers
{
	public static class MvcUrlHelper
	{
		public static string ControllerName(this HtmlHelper helper)
		{
			return (string)helper.ViewContext.RouteData.Values["controller"]; 
		}

		public static string ActionName(this HtmlHelper helper)
		{
			return (string)helper.ViewContext.RouteData.Values["action"];
		}

		public static string GetControllerName(this HtmlHelper helper, string url)
		{
			url = MapTodefaultRoute(helper, url);
			string controllerName = url.Split('/')[1];
			return controllerName;
		}

		public static string GetActionName(this HtmlHelper helper, string url)
		{
			url = MapTodefaultRoute(helper, url);
			string actionName = url.Split('/')[2];
			return actionName;
		}

		public static string GetControllerTitle(this HtmlHelper helper, List<MenuItem> menuItems)
		{
			return (from o in menuItems??new List<MenuItem>()
			        where helper.GetControllerName(o.NavigateUrl).ToLower() == helper.ControllerName().ToLower()
			        select o.Text).FirstOrDefault();
		}

		public static HttpContextBase httpContext
		{
			get
			{
				HttpContextWrapper context =
					new HttpContextWrapper(HttpContext.Current);
				return context;
			}
		}

		public static string DefaultUrl(this HtmlHelper helper)
		{
            var defaultRoute = GetDefaultRouteObject(helper);
            return "/" + defaultRoute["controller"]  + "/" + defaultRoute["action"] ;
		}

		public static RouteValueDictionary GetDefaultRouteObject(HtmlHelper helper)
		{
			return (from Route route in helper.RouteCollection let routeData = route.GetRouteData(helper.ViewContext.HttpContext) where routeData != null select route.Defaults).FirstOrDefault();
		}

		public static string MapTodefaultRoute(HtmlHelper helper, string url)
		{
			var currentUrl = url;
			var currentUrlSubStr = currentUrl.Split('/');
			var controllerName = currentUrlSubStr.ElementAtOrDefault(1);
			var actionName = currentUrlSubStr.ElementAtOrDefault(2);


			var defaultRoute = GetDefaultRouteObject(helper);
			url = "/" + (String.IsNullOrEmpty(controllerName) ? defaultRoute["controller"] : controllerName) +
			      "/" + (String.IsNullOrEmpty(actionName) ? defaultRoute["action"] : actionName);

			return url;
		}

		public static string MapPath(string path)
		{
			return HttpContext.Current.Server.MapPath(path);
		}

		public static bool IsSameControllerAndAction(this HtmlHelper helper, MenuItem menuItem, out bool insideSubItem)
		{
			insideSubItem = InSideSubMenuItem(helper, menuItem);
			return IsSameController(helper, menuItem.NavigateUrl) &&
			       IsSameAction(helper, menuItem.NavigateUrl) && 
				   (menuItem.MenuItems == null || menuItem.MenuItems.Count == 0);
		}

		public static bool IsSameAction(this HtmlHelper helper, string url)
		{
			url = MapTodefaultRoute(helper, url);

			return CompareUtility.IsStringsEqual(ActionName(helper), GetActionName(helper, url));
		}

		public static bool IsSameController(this HtmlHelper helper, string url)
		{
			url = MapTodefaultRoute(helper, url);

			return CompareUtility.IsStringsEqual(ControllerName(helper), GetControllerName(helper, url));
		}

		public static bool IsSameController(this HtmlHelper helper, MenuItem menuItem, out bool insideSubItem)
		{
			insideSubItem = InSideSubMenuItem(helper, menuItem); 
			var url = menuItem.NavigateUrl;
			bool parentIsSame = CompareUtility.IsStringsEqual(ControllerName(helper), GetControllerName(helper, url));
			return parentIsSame && ( menuItem.MenuItems== null || menuItem.MenuItems.Count == 0);
		}

		public static bool InSideSubMenuItem(this HtmlHelper helper, MenuItem menuItem)
		{
			foreach (var subItem in menuItem.MenuItems ?? new List<MenuItem>())
			{
				if (IsSameController(helper, subItem.NavigateUrl) && subItem.NavigateUrl != "#")
				{
					return true;
				}
			}
			return false;
		}
	}
}